﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Enterprise.Infrastructure;

using Enterprise.Control;
using Enterprise.Model.CaiWu;
using Enterprise.Service.Caiwubu;
namespace Enterprise.UI.Web.Modules.CaiWuBu
{
    public partial class CWKHB : Enterprise.Service.BasePage
    {
        protected int year = (int)Utility.sink("year", Utility.MethodType.Get, 0, 0, Utility.DataType.Int);
        protected int month = (int)Utility.sink("month", Utility.MethodType.Get, 0, 0, Utility.DataType.Int);
        protected string dw = (string)Utility.sink("dw", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (year == 0)
            {
                year = DateTime.Now.Year;
            }
            if (month == 0)
            {
                month = DateTime.Now.Month;
            }
            if (string.IsNullOrEmpty(dw))
            {
                dw = "SZK";
            }
            if (!IsPostBack)
            {
                OnStart();
                OnBindDrop();
                OnBindData();
            }
        }

        private void OnStart()
        {
            CreateBT.AddButton("back.gif", "返回", "Index.aspx", Utility.PopedomType.List, HeadMenu1);
            CreateBT.AddButton("a.gif", "设置年度计划数", "SetJh.aspx", Utility.PopedomType.Edit, HeadMenu1);
        }

        private void OnBindData()
        {
            CwTypeService tService = new CwTypeService();
            CwZbService zService=new CwZbService();
            var tlist = tService.CwTypeList();
            List<CwZbEntity> list = new List<CwZbEntity>();
            foreach (CwTypeEntity entity in tlist)
            {
                list.AddRange(zService.CwZbList().Where(p => p.ZBTYPEID == entity.TYPEID));
            }
            GridView1.DataSource = list;
            GridView1.DataBind();

            Utility.GroupRows(GridView1, 0);
        }

        //private void BindGrid()
        //{
        //    //如果没有数据生成空表
        //    Enterprise.Service.Caiwubu.KHBService khbservice = new Service.Caiwubu.KHBService();
        //    khbservice.Add(ddl_nf.SelectedValue, ddl_yf.SelectedValue);            
        //    //绑定数据
        //    Enterprise.Service.GridViewHelper.GvHelper gvh = new GvHelper();
        //    gvh.BindData("cwb_khb",
        //        "t.NF='" + ddl_nf.SelectedValue + "' and t.YF='" + ddl_yf.SelectedValue + "' and t.dwmc='"+ddl_dw.SelectedValue+"'",
        //        gv);
        //}
        
        private void OnBindDrop()
        {
            int iNow = DateTime.Now.Year;
            int iPre = iNow - 2;
            int iNext = iNow + 2;
            for (int i = iPre; i <= iNext; i++)
            {
                ddl_nf.Items.Add(new ListItem(i.ToString()));
            }
            ListItem liyear = this.ddl_nf.Items.FindByValue(year.ToString());
            if (liyear != null)
                liyear.Selected = true;

            Enterprise.Infrastructure.Utility.bindMonth(ddl_yf);
            ListItem li = this.ddl_yf.Items.FindByValue(month.ToString());
            if (li != null)
                li.Selected = true;

            ListItem lidw = ddl_dw.Items.FindByValue(dw);
            if (lidw != null)
                lidw.Selected = true;
        }

        //保存
        protected void Button1_Click(object sender, EventArgs e)
        {
            string _note = "";
            try
            {
                CwZbService zbService = new CwZbService();
                CwZbkhService khService = new CwZbkhService();

                foreach (GridViewRow row in GridView1.Rows)
                {
                    int zbId = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
                    TextBox txtjhs = (TextBox)row.Cells[3].FindControl("t1");
                    decimal jsj = (decimal)Utility.sink(txtjhs.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Decimal);
                    var entity = khService.CwZbkhDisp(zbId, year, month, dw);
                    if (entity != null)
                    {
                        entity.KSJS = jsj;
                        entity.DB_Option_Action = "Update";
                    }
                    else
                    {
                        entity = new CwZbkhEntity();
                        entity.ZBID = zbId;
                        entity.KYEAR = year;
                        entity.KMONTH = month;
                        entity.KDW = dw;
                        entity.KSJS = jsj;
                        entity.KQZ = 0;
                        entity.KJHS = 0;
                        entity.DB_Option_Action = "Insert";
                    }
                    khService.CwZbkhExecute(entity, 0);
                }
                //更新完成后刷新缓存
                khService.ClearCache();
                _note = "更新成功！";
            }
            catch (Exception ex)
            {
                _note = ex.Message;
            }
            Utility.ShowMsg(this.Page, "OK", this.Tran(_note), "CWKHB.aspx?year=" + year + "&month=" + month + "&dw=" + dw + "");
        }

        protected void ddl_nf_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectIndexChanged();
        }

        protected void ddl_yf_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectIndexChanged();
        }

        protected void ddl_dw_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectIndexChanged();
        }

        private void SelectIndexChanged()
        {
            int _year = (int)Utility.sink(ddl_nf.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Int);
            int _month = (int)Utility.sink(ddl_yf.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Int);
            string _dw = (string)Utility.sink(ddl_dw.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
            Response.Redirect("CWKHB.aspx?year=" + _year + "&month=" + _month + "&dw=" + _dw + "");
            Response.End();
        }

    }
}